//
//  ApplePay+Extension.swift
//  gongchengyan
//
//  Created by wang shuguan on 2020/4/26.
//  Copyright © 2020 wsg. All rights reserved.
//

import UIKit
import XYIAPKit
extension UIViewController {

    
    func gotoRenewal(price:Float = -1){
        if Preference.isChecking{
            //TODO: 未处理用户异常退出的问题，造成的虽然支付了，但是没有处理会员信息
            guard (Preference.token ?? "").isNotEmpty else {
                NotificationCenter.default.post(name: .userLogout, object: "stay")
                return
            }
            DispatchQueue.main.async {
                UIScreen.startFullAnimation(text: "正在访问苹果支付")
            }
            XYStore.default()?.requestProducts(["com.jianqi.gongchengyan.p_4"], success: { (products, invalidProductIdentifiers) in
                if let p = products?.first as? SKProduct{
                    DispatchQueue.main.async {
                        UIScreen.startFullAnimation(text: "正在支付")
                    }
                    XYStore.default()?.addPayment(p.productIdentifier, success: {[weak self] (tra) in
                        
                        DispatchQueue.main.async {
                            UIScreen.stopFullAnimation()
                        }
                        if tra?.transactionState == .purchased,let SSelf = self{
                            #if DEBUG
                            let isSandbox = true
                            #else
                            let isSandbox = false
                            #endif
                            GCY.requestZWT("/ApplePay/PayCallback".withApi.para(["appleReceipt":tra?.transactionIdentifier ?? "","isSandbox":isSandbox,"userId":"15036379106@139.com"]),name:SSelf.runningPath ?? SSelf.simpleIdentifier).responseZWTJSON { response,_,isSuccesss in
                                if isSuccesss{
                                    NotificationCenter.default.post(name: .needUserInfoChanged, object: nil)
                                }
                            }
                        }
                    }, failure: { (tra, error) in
                        DispatchQueue.main.async {
                            UIScreen.stopFullAnimation()
                            UIAlertController.alertMessage("支付失败", subtitle: error?.localizedDescription ?? "", btnTitles: ["好的"]) { (index) in
                                
                            }
                        }
                    })
                }else{
                    DispatchQueue.main.async {
                        UIScreen.stopFullAnimation()
                    }
                }
            }, failure: { (error) in
                DispatchQueue.main.async {
                    UIScreen.stopFullAnimation()
                    UIAlertController.alertMessage("支付失败", subtitle: error?.localizedDescription ?? "", btnTitles: ["好的"]) { (index) in
                        
                    }
                }
            })
        }else{
            let vc = MeVIPPayViewController(hidesBottomBarWhenPushed: true, nibName: MeVIPPayViewController.identifier)
            vc.activePrice = price
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
}
